<template>
  <div class="page">
    <div class="banner">
      <img src="@/assets/banner.png" alt="">
    </div>
    <div class="banner">
      <img src="@/assets/info.png" alt="">
    </div>

    <div class="content">

      <div class="title">[填写真实信息，有助于成功领取资料]</div>

      <div class="cell_box">
        <div class="cell">
          <div class="name">姓名：</div>
          <div class="txt">
            <input type="text" v-model="addForm.name" class="a_input" />
          </div>
        </div>
        <div class="cell">
          <div class="name">年龄：</div>
          <div class="txt">
            <input type="tel" v-model="addForm.age" maxlength="2" class="a_input" />
          </div>
        </div>
        <div class="cell">
          <div class="name">职业信息：</div>
          <div class="txt tag_list">
            <div class="tag" :class="active == item.id?'active':''" v-for="item in menus" :key="item.id" @click="menuEvent(item)">{{item.name}}</div>
          </div>
        </div>
        <div class="cell">
          <div class="name">地区选择：</div>
          <div class="txt tag_list">
            <div class="tag active">广东</div>
          </div>
        </div>
        <div class="cell">
          <div class="name">电话号码：</div>
          <div class="txt">
            <input type="text" v-model="addForm.mobile" class="a_input" />
          </div>
        </div>
      </div>

      <div class="btn_group">
        <button class="btn" @click="saveInfo">点击免费领取教材</button>
      </div>

    </div>

    <div class="footer">
      <p>商务合作：13715399505</p>
      <p>备案号：粤ICP备2021034062号</p>
    </div>

  </div>
</template>
<script>
import { showToast, showModal, checkPhone, getUrlParams } from "@/common/util";
import {
  ACCOUNTINGAPPLY,
  MARKETINGADD,
  SENDMARKETINGREPORTING,
} from "@/http/interface";
import { Toast } from "vant";
export default {
  data() {
    return {
      addForm: {},
      menus: [
        { name: "在校生", id: 1 },
        { name: "财会在职人员", id: 2 },
      ],
      active: 1,
      adId: "",
    };
  },
  mounted() {
    this.send();
  },
  methods: {
    //
    menuEvent(row) {
      this.active = row.id;
    },

    // 保存数据
    saveInfo() {
      let _this = this;
      let o = this.addForm;

      if (!o.name) {
        showModal({
          message: "请输入姓名",
        });
        return false;
      }

      if (!o.age) {
        showModal({
          message: "请输入年龄",
        });
        return false;
      }

      if (!o.mobile) {
        showModal({
          message: "请输入电话号码",
        });
        return false;
      }

      if (!checkPhone(o.mobile)) {
        showModal({
          message: "请输入正确的电话号码",
        });
        return false;
      }
      let oParams = this.addForm || {};
      oParams.occupation = this.active;
      oParams.area = "广东";

      ACCOUNTINGAPPLY(oParams).then((res) => {
        if (res.code != 0) {
          showModal({
            message: res.msg||res.message,
          });
          return false;
        }

        _this.toReport()
        if (!res.data || res.data == null) {
          showModal({
            message: "请勿重复提交",
            beforeClose: function (done) {
              done()
              _this.$router.push({ name: "result" });
            },
          });
          return false;
        }
        localStorage.setItem("weixin", res.data);
        _this.$router.push({ name: "result", query: { id: res.data } });
      });
    },

    /** 进入首页 埋点 */
    send() {
      let _this = this;
      let o = getUrlParams();
      let oParams = {};
      let base64 = this.toBase64();
      oParams.accountId = o.accountId;
      oParams.channel = o.channel;
      oParams.urlBase64 = base64;
      oParams.clickId = o.gdt_vid;
      console.log(oParams);
      MARKETINGADD(oParams).then((res) => {
        if (res.code != 0) {
          Toast(res.message || res.msg || "出错了，请联系管理员");
          return false;
        }
        _this.adId = res.o;
      });
    },

    /** 获取base64 */
    toBase64() {
      let url = window.location.href;
      let n = encodeURI(url);
      var base64 = btoa(n);
      return base64;
    },

    /** 广告上报 */
    toReport() {
      let _this = this;
      let o = getUrlParams();
      let oParams = {};
      let base64 = this.toBase64();
      // oParams.accountId = o.accountId;
      // oParams.channel = o.channel;
      // oParams.urlBase64 = base64;
      // oParams.clickId = o.gdt_vid;
      // console.log(oParams);
      oParams.mId = this.adId;
      SENDMARKETINGREPORTING(oParams);
    },
  },
};
</script>
<style lang="less" scoped>
img {
  pointer-events: none;
}
.banner {
  img {
    width: 100%;
  }
}
.content {
  .title {
    text-align: center;
    margin: 20px 0;
  }
  .cell_box {
    padding: 10px 0;
    margin: 10px;
    background: #fff;
    box-shadow: 1px 1px 4px 0 #ccc;
    .cell {
      display: flex;
      align-items: center;
      padding: 0 10px;
      margin: 16px 0;
      .name {
        font-size: 14px;
      }
      .a_input {
        background: #e9ebea;
        border: 0;
        box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.2);
        height: 30px;
        padding: 0 10px;
        box-sizing: border-box;
        border-radius: 2px;
      }
      .tag_list {
        display: flex;
        align-items: center;
        .tag {
          font-size: 14px;
          background: #e9ebea;
          padding: 2px 10px;
          margin: 0 4px;
          border-radius: 4px;
        }
        .active {
          background: #ae2626;
          color: #fff;
        }
      }
    }
  }
}
.btn_group {
  text-align: center;
  padding: 20px 0 40px 0;
  .btn {
    background: #ae2626;
    border: 0;
    color: #fff;
    border-radius: 4px;
    height: 38px;
    padding: 0 20px;
  }
}
.footer {
  font-size: 16px;
  text-align: center;
  margin: 60px 0 16px 0;
  p {
    margin: 0;
    padding: 0;
    line-height: 1.5;
    color: #333;
  }
}
</style>